package gui;

import java.util.ArrayList;

import javax.swing.table.DefaultTableModel;

import controller.StatusSeat;


public class SaleTableModel extends DefaultTableModel 
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ArrayList<StatusSeat> wantedSeats;
	
	public SaleTableModel()
	{
		wantedSeats = new ArrayList<StatusSeat>();
		initTable();
	}
	
	public void addTicket(StatusSeat seat)
	{
		wantedSeats.add(seat);
		redrawTable();
	}
	
	public void removeTicket(StatusSeat seat)
	{
		wantedSeats.remove(seat);
		redrawTable();
	}
	
	public StatusSeat getTicket(int rowIndex)
	{
		return wantedSeats.get(rowIndex);
	}
	
	public ArrayList<StatusSeat> getAllTickets()
	{
		return wantedSeats;
	}
	
	/**
	 * Removes all held data in this table
	 */
	public void clearTable()
	{
		wantedSeats = new ArrayList<StatusSeat>();
		redrawTable();
	}
	
	private void initTable()
	{
		setColumnCount(3);
		setRowCount(1);
	}
	
	/** Redraws the table.
	 *  Because we don't know, if an element in the middle of the
	 *  ArrayList has been removed, we need to redraw the entire table
	 *  every time a change occurs.
	 */
	private void redrawTable()
	{
		setRowCount(wantedSeats.size());	//<-- just in case
		
		for(int currentRow = 0; currentRow < wantedSeats.size(); currentRow++) {
			StatusSeat seat = wantedSeats.get(currentRow);
			
			setValueAt(seat.getCinema().getNumber(), currentRow, 0);
			setValueAt(seat.getSeat().getRow(), currentRow, 1);
			setValueAt(seat.getSeat().getNumber(), currentRow, 2);
		}
	}
}
